import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/components/Login";
import Home from "@/components/Home";
import Welcome from "@/components/Welcome";
import Users from "@/components/User/Users";
import Right from "@/components/power/Right";
import el from "element-ui/src/locale/lang/el";
import Roles from "@/components/power/Roles";

Vue.use(VueRouter)
//抛出异常=>解决报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)

}



const router = new VueRouter({
    routes:[
        {path: '/', redirect: '/login'},
        {path: '/login', component: Login},
        {path: '/home', component: Home,redirect:'/welcome',meta:{title:'首页'},children:
                [
                    {path:'/welcome', component:Welcome,meta:{title:'首页'}},
                    {path:'/users',component:Users,meta:{title:'用户列表'}},
                    {path:'/rights',component:Right,meta:{title: '权限列表'}},
                    {path:'/Roles',component:Roles,meta:{title: '角色列表'}}

                ]
        },

    ]
})
// 路由守卫
router.beforeEach((to, from, next) => {
    let isLogin = window.sessionStorage.getItem('token');
    if (isLogin) {
        //如果用户信息存在则往下执行。
        next()
    } else {
        //如果用户token不存在则跳转到login页面
        if (to.path === '/login') {
            next()
        } else {
            next('/login')
        }
    }
    next()
})

export default router
